module.exports = {
  schedule: {
    cron: '0 0 1 * * ?',
    type: 'worker',
    env: ['pachong'],
    // immediate: true
  },

  async task(ctx) {
    ctx.logger.info('taosha clean begin...');
    const authors = ['环球网', '新京报', '观察者网', '今日头条', '网易新闻', '青蜂侠Bee', 'Bilibili热榜'];

    // // 先清一些新闻网站3天前的数据
    // let date = new Date();
    // date.setDate(date.getDate() - 3);
    // await ctx.model.Topic.remove({author_name: /新闻$/, tab: 'yutang', createdAt: {$lt: date}});
    // await ctx.model.Topic.remove({author_name: {'$in': authors}, tab: 'yutang', createdAt: {$lt: date}});

    // const all_users = await ctx.model.User.find({loginname: {'$in': authors}});
    const all_users = await ctx.model.User.find();

    for (let i = 0; i < all_users.length; i++) {
      const user = all_users[i];

      const topic_count = await ctx.model.Topic.count({
        tab: {$ne: 'share'},
        is_public: true,
        author_id: user.id
      });

      await ctx.model.User.updateOne({_id: user.id}, {
        $set: {
          topic_count: topic_count
        },
      });

    }


    ctx.logger.info('taosha clean end...');
  }
};
